/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Game.java
 *
 * Created on 21-feb-2012, 9:58:26
 */
package mastermind;

import javax.swing.JLabel;

/**
 *
 * @author alumno
 */
public class Game extends javax.swing.JFrame {

    int maxtries = 11;
    int tries = maxtries;
    int black = 0;
    int white = 0;
    int ball1_index = 1;
    int ball2_index = 1;
    int ball3_index = 1;
    int ball4_index = 1;
    int ball1_c_index = aleatorioEntre(1, 8);
    int ball2_c_index = aleatorioEntre(1, 8);
    int ball3_c_index = aleatorioEntre(1, 8);
    int ball4_c_index = aleatorioEntre(1, 8);
    boolean lost = false;

    /** Creates new form Game */
    public Game() {
        setLocationRelativeTo(null);
        initComponents();
        setColor(ball1, ball1_index);
        setColor(ball2, ball2_index);
        setColor(ball3, ball3_index);
        setColor(ball4, ball4_index);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ball1 = new javax.swing.JLabel();
        ball2 = new javax.swing.JLabel();
        ball3 = new javax.swing.JLabel();
        ball4 = new javax.swing.JLabel();
        up1 = new javax.swing.JButton();
        up2 = new javax.swing.JButton();
        up3 = new javax.swing.JButton();
        up4 = new javax.swing.JButton();
        down1 = new javax.swing.JButton();
        down2 = new javax.swing.JButton();
        down3 = new javax.swing.JButton();
        down4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        sm4 = new javax.swing.JLabel();
        sm3 = new javax.swing.JLabel();
        sm2 = new javax.swing.JLabel();
        sm1 = new javax.swing.JLabel();
        checker = new javax.swing.JButton();
        output = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Txus' Mastermind");

        ball1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/1.png"))); // NOI18N

        ball2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/2.png"))); // NOI18N
        ball2.setText("jLabel1");
        ball2.setPreferredSize(new java.awt.Dimension(32, 32));

        ball3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/3.png"))); // NOI18N
        ball3.setText("jLabel1");
        ball3.setPreferredSize(new java.awt.Dimension(32, 32));

        ball4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/4.png"))); // NOI18N
        ball4.setText("jLabel1");
        ball4.setPreferredSize(new java.awt.Dimension(32, 32));

        up1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                up1ActionPerformed(evt);
            }
        });

        up2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                up2ActionPerformed(evt);
            }
        });

        up3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                up3ActionPerformed(evt);
            }
        });

        up4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                up4ActionPerformed(evt);
            }
        });

        down1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                down1ActionPerformed(evt);
            }
        });

        down2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                down2ActionPerformed(evt);
            }
        });

        down3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                down3ActionPerformed(evt);
            }
        });

        down4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                down4ActionPerformed(evt);
            }
        });

        sm4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/Gray.png"))); // NOI18N

        sm3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/Gray.png"))); // NOI18N

        sm2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/Gray.png"))); // NOI18N

        sm1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/Gray.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(sm1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sm2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(sm3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sm4)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sm2)
                    .addComponent(sm1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sm3)
                    .addComponent(sm4))
                .addContainerGap())
        );

        checker.setText("Check");
        checker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkerActionPerformed(evt);
            }
        });

        output.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        output.setText("Tries left: 11");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(up1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ball1)
                    .addComponent(down1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(up2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ball2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(down2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(up3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ball3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(down3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(up4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ball4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(down4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(output, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(checker)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(up1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(ball1)
                        .addGap(6, 6, 6)
                        .addComponent(down1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(up2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(ball2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(down2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(up3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(ball3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(down3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(up4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(ball4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(down4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(output, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(checker, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void up1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_up1ActionPerformed
    if (upColor(ball1_index)) {
        ball1_index++;
    }
    setColor(ball1, ball1_index);
}//GEN-LAST:event_up1ActionPerformed

private void up2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_up2ActionPerformed
    if (upColor(ball2_index)) {
        ball2_index++;
    }
    setColor(ball2, ball2_index);
}//GEN-LAST:event_up2ActionPerformed

private void up3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_up3ActionPerformed
    if (upColor(ball3_index)) {
        ball3_index++;
    }
    setColor(ball3, ball3_index);
}//GEN-LAST:event_up3ActionPerformed

private void up4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_up4ActionPerformed
    if (upColor(ball4_index)) {
        ball4_index++;
    }
    setColor(ball4, ball4_index);
}//GEN-LAST:event_up4ActionPerformed

private void down1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_down1ActionPerformed
    if (downColor(ball1_index)) {
        ball1_index--;
    }
    setColor(ball1, ball1_index);
}//GEN-LAST:event_down1ActionPerformed

private void down2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_down2ActionPerformed
    if (downColor(ball2_index)) {
        ball2_index--;
    }
    setColor(ball2, ball2_index);
}//GEN-LAST:event_down2ActionPerformed

private void down3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_down3ActionPerformed
    if (downColor(ball3_index)) {
        ball3_index--;
    }
    setColor(ball3, ball3_index);
}//GEN-LAST:event_down3ActionPerformed

private void down4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_down4ActionPerformed
    if (downColor(ball4_index)) {
        ball4_index--;
    }
    setColor(ball4, ball4_index);
}//GEN-LAST:event_down4ActionPerformed

private void checkerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkerActionPerformed
    if (lost) {
        restart();
    } else {
        black = 0;
        white = 0;
        if (ball1_index == ball1_c_index) {
            black++;
        } else {
            if (ball1_index == ball2_c_index) {
                white++;
            }
            if (ball1_index == ball3_c_index) {
                white++;
            }
            if (ball1_index == ball4_c_index) {
                white++;
            }
        }

        if (ball2_index == ball2_c_index) {
            black++;
        } else {
            if (ball2_index == ball1_c_index) {
                white++;
            }
            if (ball2_index == ball3_c_index) {
                white++;
            }
            if (ball2_index == ball4_c_index) {
                white++;
            }
        }

        if (ball3_index == ball3_c_index) {
            black++;
        } else {
            if (ball3_index == ball1_c_index) {
                white++;
            }
            if (ball3_index == ball2_c_index) {
                white++;
            }
            if (ball3_index == ball4_c_index) {
                white++;
            }
        }

        if (ball4_index == ball4_c_index) {
            black++;
        } else {
            if (ball4_index == ball1_c_index) {
                white++;
            }
            if (ball4_index == ball2_c_index) {
                white++;
            }
            if (ball4_index == ball4_c_index) {
                white++;
            }
        }

        setSmallBalls();
        decTries();
    }
}//GEN-LAST:event_checkerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Game().setVisible(true);
            }
        });
    }

    private static int aleatorioEntre(int min, int max) {
        return (int) (min + (max - min + 1) * (Math.random()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ball1;
    private javax.swing.JLabel ball2;
    private javax.swing.JLabel ball3;
    private javax.swing.JLabel ball4;
    private javax.swing.JButton checker;
    private javax.swing.JButton down1;
    private javax.swing.JButton down2;
    private javax.swing.JButton down3;
    private javax.swing.JButton down4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel output;
    private javax.swing.JLabel sm1;
    private javax.swing.JLabel sm2;
    private javax.swing.JLabel sm3;
    private javax.swing.JLabel sm4;
    private javax.swing.JButton up1;
    private javax.swing.JButton up2;
    private javax.swing.JButton up3;
    private javax.swing.JButton up4;
    // End of variables declaration//GEN-END:variables

    private void setColor(JLabel ball, int index) {
        ball.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/" + index + ".png")));
    }

    private boolean downColor(int index) {
        if (index > 1) {
            return true;
        }
        return false;
    }

    private boolean upColor(int index) {
        if (index < 8) {
            return true;
        }
        return false;
    }

    private void setSmallBalls() {
        customSetIcon(sm1, "gray");
        customSetIcon(sm2, "gray");
        customSetIcon(sm3, "gray");
        customSetIcon(sm4, "gray");
        switch (black + white) {
            case 4:
                customSetIcon(sm4, "white");
            case 3:
                customSetIcon(sm3, "white");
            case 2:
                customSetIcon(sm2, "white");
            case 1:
                customSetIcon(sm1, "white");
        }
        switch (black) {
            case 4:
                customSetIcon(sm4, "black");
                output.setText("You have won!");
                disableButtons();
            case 3:
                customSetIcon(sm3, "black");
            case 2:
                customSetIcon(sm2, "black");
            case 1:
                customSetIcon(sm1, "black");
        }
    }

    private void decTries() {
        tries--;
        if (tries >= 0) {
            output.setText("Tries left: " + tries);
        } else {
            disableButtons();
            output.setText("You have lost!");
        }
    }

    private void disableButtons() {
        lost = true;
        up1.setEnabled(false);
        up2.setEnabled(false);
        up3.setEnabled(false);
        up4.setEnabled(false);
        down1.setEnabled(false);
        down2.setEnabled(false);
        down3.setEnabled(false);
        down4.setEnabled(false);
        checker.setText("Retry");
    }

    private void restart() {
        lost = false;
        up1.setEnabled(true);
        up2.setEnabled(true);
        up3.setEnabled(true);
        up4.setEnabled(true);
        down1.setEnabled(true);
        down2.setEnabled(true);
        down3.setEnabled(true);
        down4.setEnabled(true);
        checker.setText("Check");
        output.setText("Tries left: " + maxtries);
        tries = maxtries;
        black = 0;
        white = 0;
        ball1_index = 1;
        ball2_index = 1;
        ball3_index = 1;
        ball4_index = 1;
        setColor(ball1, ball1_index);
        setColor(ball2, ball2_index);
        setColor(ball3, ball3_index);
        setColor(ball4, ball4_index);
        ball1_c_index = aleatorioEntre(1, 8);
        ball2_c_index = aleatorioEntre(1, 8);
        ball3_c_index = aleatorioEntre(1, 8);
        ball4_c_index = aleatorioEntre(1, 8);
        lost = false;
    }

    private void customSetIcon(JLabel icon, String picture) {
        icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pics/" + picture + ".png")));
    }
}
